package elven.codingit.easyutil.lucene;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.alibaba.fastjson.JSON;

import elven.codingit.easyutil.common.LogFactory;

public class Indexer {

	public List<Map<String, Object>> search(String keyword){
		List<Map<String, Object>> results = new ArrayList<>();
		try {
			//文件型索引路径
			Path indexerPath = Paths.get("E:\\repository\\lucene\\indexer\\demo");
			Directory directory = FSDirectory.open(indexerPath);
			DirectoryReader directoryReader = DirectoryReader.open(directory);
			//分词器
			Analyzer analyzer = new StandardAnalyzer();
			//索引查询api
			IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
			//要查询的字段，对应写入索引文件的Field
			String[] fields = {"id", "icnName"};
			MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer);
			//设置搜索的关键词
			Query query = parser.parse(keyword);
			ScoreDoc[] hits = indexSearcher.search(query, 100).scoreDocs;
			for(int i = 0; i < hits.length; i++){
				Map<String, Object> data = new HashMap();
				Document document = indexSearcher.doc(hits[i].doc);
				for(String temp : fields){
					data.put(temp, document.get(temp));
				}
				results.add(data);
			}
			directoryReader.close();
			directory.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}
	
	public void builder(){
		try {
			Path indexerPath = Paths.get("E:\\repository\\lucene\\indexer\\demo");
			Directory directory = FSDirectory.open(indexerPath);//文件型索引
			Analyzer analyzer = new StandardAnalyzer();//分词器,有多种分词器,standard用来为每个中文字符创建索引
			IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
			IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
			String[] testname = "美D,将军,讲师,僵尸".split(",");
			for(int i = 0; i < 100; i ++){
				Document idoc = new Document();
				Field icnId = new Field("id", (""+i), TextField.TYPE_STORED);
				Field icnName = new Field("icnName", (""+i+testname[new Random().nextInt(testname.length)]), TextField.TYPE_STORED);
				idoc.add(icnId);
				idoc.add(icnName);
				indexWriter.addDocument(idoc);
			}
			indexWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static void main(String[] args) {
		try {
			Indexer index = new Indexer();
			//index.builder();
			LogFactory.d().info(JSON.toJSONString(index.search("王五")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
